package com.zj.leetcode.easy.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @program algorithm
 * @description:
 * @author: zhangbo
 * @create: 2022/06/11 23:02
 */
public class PreorderTraversal {


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        List<Integer> list = new ArrayList<>();

        public List<Integer> preorderTraversal(TreeNode root) {

            this.pre(root);
            return list;

        }


        public int pre(TreeNode node) {
            if (node != null) {
                list.add(node.val);

                if (node.left != null) {
                    this.pre(node.left);
                }

                if (node.right != null) {
                    this.pre(node.right);
                }

                return node.val;
            }

            return 0;


        }
    }
}
